package edu.mju.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import edu.mju.commonutils.ReturnCode;
import edu.mju.servicebase.exception.StuException;
import edu.mju.vod.service.VodService;
import edu.mju.vod.utils.ConstVodUtils;
import edu.mju.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:wyp
 * @Date:2020/9/13 17:45
 */

@RestController
@RequestMapping("/vodService/video")
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     *  上传到阿里云
     * @param file 上传的文件
     * @return
     */
    @PostMapping("/uploadVideo")
    public ReturnCode uploadVideo(MultipartFile file){
        //返回上传视频的id
        String videoId = vodService.uploadVideo(file);
        Map<String,Object> map = new HashMap<>();
        map.put("videoId",videoId);
        return ReturnCode.success().data(map);
    }

    /**
     * 根据视频id删除视频
     * @param videoId 视频id
     * @return
     */
    @DeleteMapping("/removeVideo/{videoId}")
    public ReturnCode removeVideo(@PathVariable String videoId){
        try {
            //初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(ConstVodUtils.KEY_ID, ConstVodUtils.KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(videoId);
            //使用初始化对象的方法实现删除
            defaultAcsClient.getAcsResponse(request);
            return ReturnCode.success();
        }catch (Exception e){
            e.printStackTrace();
            throw new StuException(20001,"删除视频失败");
        }
    }

    /**
     * 删除多个视频的方法
     * @param videoIdList 视频id列表
     * @return
     */
    @DeleteMapping("/deleteAllVideo")
    public ReturnCode deleteAllVideo(@RequestParam("videoList") List<String> videoIdList){
        vodService.removeAllVideo(videoIdList);
        return ReturnCode.success();

    }

    /**
     * 根据视频id获取视频凭证
     * @param videoId
     * @return
     */
    @GetMapping("/getVideo/{videoId}")
    public ReturnCode getVideo(@PathVariable String videoId){
        try {
            //创建初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(ConstVodUtils.KEY_ID, ConstVodUtils.KEY_SECRET);
            //创建获取凭证request respon对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

            //设置视频id
            request.setVideoId(videoId);

            //调用方法得到凭证
            GetVideoPlayAuthResponse acsResponse = defaultAcsClient.getAcsResponse(request);
            String playAuth = acsResponse.getPlayAuth();
            Map<String, Object> map = new HashMap<>();
            map.put("playAuth",playAuth);
            return ReturnCode.success().data(map);

        }catch (Exception e){
            throw new StuException(20001,"获取视频凭证失败！");
        }
    }
}
